package com.star.spring.auth.authtication;

import com.star.spring.auth.model.UserEntity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class MyUserDetailsService implements UserDetailsService {


  // 自定义认证逻辑
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    // 去数据库中查询用户
    UserEntity userEntity = new UserEntity();
    userEntity.setUsername("star");
    userEntity.setNickname("starlit");
    userEntity.setPassword("$2a$10$TqdkCB.mawJF0qTecuzQ/OLzoVQt9UnGkJEjjCI7jalwAT8HBwKHa");

    // 封装为UserDetails对象
    UserDetails userDetails = User
         .withUsername(userEntity.getUsername())
         .password(userEntity.getPassword())
         .authorities("admin")
         .build();
    // 4.返回封装好的UserDetails对象
    return userDetails;
   }
}
